package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.LibListBean;
import cn.bocweb.spinning.model.Bean.MyClassBean;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.view.activity.TestDetailActivity;
import cn.bocweb.spinning.view.common.MyAdapter;

/**
 * Created by chenchangxing on 16/1/27.
 */
public class MySchoolAdapter extends MyAdapter<MyClassBean.RecordEntity> {


    public MySchoolAdapter(Context context, List<MyClassBean.RecordEntity> list) {
        super(context, list);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_my_school, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        int min = Integer.parseInt(list.get(position).getSpend_time()) / 60;
        int sec = Integer.parseInt(list.get(position).getSpend_time()) % 60;
        String time = min + "分" + sec + "秒";

        LibListBean.DataEntity lib = new LibListBean.DataEntity();
        lib.readLibListIntoLocal(context);
        holder.scoreTitle.setText(lib.getTitle());

        holder.scoreLevel.setText(list.get(position).getSubtitle());
        holder.score.setText(list.get(position).getScore());
        holder.scoreUsedTime.setText(time);

        holder.btnScoreDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, TestDetailActivity.class);
                intent.putExtra(Constant.TEST_DETAIL, list.get(position).getTestid());
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_my_school.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.score_title)
        TextView scoreTitle;
        @Bind(R.id.score_level)
        TextView scoreLevel;
        @Bind(R.id.score)
        TextView score;
        @Bind(R.id.score_used_time)
        TextView scoreUsedTime;
        @Bind(R.id.btn_score_detail)
        RelativeLayout btnScoreDetail;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
